package com.pengzy.core.security;

import com.pengzy.comment.core.utils.JsonUtils;
import com.pengzy.comment.web.result.JsonResult;
import com.pengzy.comment.web.result.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**AuthenticationEntryPoint：认证异常失败处理
 *
 *
 * @author xiaozhi
 */
@Component
@Slf4j
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.error("用户认证失败！请联系管理员");
        ServletUtils.renderString(response, JsonUtils.toJsonString(JsonResult.error(401,"用户认证失败！请联系管理员")));
    }

}
